package view;

import bean.Detail;
import bean.GoodInfo;
import dao.DetailDao;
import dao.GoodInfoDao;
import impl.DetailDaoImpl;
import impl.GoodInfoDaoImpl;
import util.InputUtil;
import util.ServerResponseResult;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @classname:GoodInfoView
 * @description:
 * @author: Mr.Z
 * @date: 2021/1/1 14:59
 * @version: 0.1
 * @since:1.8
 */
public class GoodInfoView {
    private static GoodInfoDao goodinfoDao;
    private static int result;

    static {
        goodinfoDao = new GoodInfoDaoImpl();
    }

    private GoodInfoView() {
    }

    public static ServerResponseResult addGoodInfo() {
        System.out.println("商品类型表如下,请根据商品类型表来增加商品信息");
        ProductView.showAll();
        GoodInfo goodInfo = new GoodInfo();
        System.out.println("请输入商品名称");
        goodInfo.setGname(InputUtil.inputNextStr());
        System.out.println("请输入商品所属类型（根据商品类型表不可为父类）");
        goodInfo.setPid(InputUtil.inputInt());
        System.out.println("请输入商品数量");
        goodInfo.setNumber(InputUtil.inputInt());
        System.out.println("请输入商品单价");
        goodInfo.setPrice(InputUtil.inputFloat());
        System.out.println("请输入商品状态1.正常 2.下架 3.删除");
        goodInfo.setGstatus(InputUtil.inputInt());
        System.out.println("请输入商品折扣");
        goodInfo.setDiscount(InputUtil.inputFloat());
        result = goodinfoDao.addGood(goodInfo);
        if (result == 1) {
            return ServerResponseResult.success();
        }
        return ServerResponseResult.error();
    }

    //展示所有信息如果在库的情况下
    public static void showAll() {
        goodinfoDao.selectAll().forEach(good -> {
            if (good.getGstatus() == 1) {
                System.out.println(good);
            }
        });
    }

    public static void selectGoodByParams() {
        System.out.println("请选择要查询的字段\n" +
                "1.商品名 2.剩余库存 3.价格 4.商品状态(1,2,3)");
        String params = InputUtil.inputNextStr();
        String[] splitStr = params.split(",");
        Map<String, Object> map = new LinkedHashMap<>(16);
        for (String s : splitStr) {
            int num = Integer.parseInt(s);
            System.out.println("请输入新的字段数据");
            switch (num) {
                case 1:
                    map.put("gname", InputUtil.inputNextStr());
                    break;
                case 2:
                    map.put("number", InputUtil.inputInt());
                    break;
                case 3:
                    map.put("price", InputUtil.inputFloat());
                    break;
                case 4:
                    map.put("gstatus", InputUtil.inputInt());
                    break;
            }
        }
        goodinfoDao.selectGoodByParams(map).forEach(System.out::println);
    }

    public static ServerResponseResult updateGoodInfobyParams() {
        System.out.println("请输入您要修改的商品id");
        int id = InputUtil.inputInt();

        GoodInfo goodInfo = goodinfoDao.selectGoodById(id);
        System.out.println("请输入您想修改的内容\n" +
                "1.商品名 2.商品所属类型 3.商品数量 4.商品单价 5.商品状态 6.折扣");
        String str = InputUtil.inputNextStr();
        String[] split = str.split(",");
        for (String s : split) {
            int num = Integer.valueOf(s);
            System.out.println("请输入要修改的内容");
            switch (num) {
                case 1:
                    goodInfo.setGname(InputUtil.inputNextStr());
                    break;
                case 2:
                    ProductView.showAll();
                    System.out.println("请根据以上信息选择商品所属类型");
                    goodInfo.setPid(InputUtil.inputInt());
                    break;
                case 3:
                    goodInfo.setNumber(InputUtil.inputInt());
                    break;
                case 4:
                    goodInfo.setPrice(InputUtil.inputFloat());
                    break;
                case 5:
                    goodInfo.setGstatus(InputUtil.inputInt());
                    break;
                case 6:
                    goodInfo.setDiscount(InputUtil.inputFloat());
                    break;
            }
        }
        int i = goodinfoDao.updateGoodInfoById(goodInfo, id);
        if (i == 1) {
            return ServerResponseResult.success();
        }
        return ServerResponseResult.error();
    }


    public static ServerResponseResult deleteGoodRelyOrFake() {
        System.out.println("请选择真正删除商品信息/下架商品(1/2)");
        int result = InputUtil.inputInt();
        if (result == 1) {
            System.out.println("请选择要删除的id");
            GoodInfoDao goodInfoDao = new GoodInfoDaoImpl();
            int id = InputUtil.inputInt();
            DetailDao detailDao = new DetailDaoImpl();
            Detail detail = detailDao.selectDetailById(id);
            if (detail != null) {
                return ServerResponseResult.error();
            }
            int i = goodInfoDao.deleteGoodById(id);
            if (i == 1) {
                return ServerResponseResult.success();
            }
        } else if (result == 2) {
            System.out.println("请选择要下架的商品");
            int id = InputUtil.inputInt();
            GoodInfo goodInfo = goodinfoDao.selectGoodById(id);
            goodInfo.setNumber(0);
            goodInfo.setPrice(0.00f);
            goodInfo.setGstatus(2);
            goodInfo.setDiscount(0.00f);
            int i = goodinfoDao.updateGoodInfoById(goodInfo, id);
            if (i == 1) {
                System.out.println("下架成功");
            }
        }
        return ServerResponseResult.error();
    }

}
